﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Project_Udrinkwater.Empoyee_Management
{
    public partial class ShowListEmp : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void bt_ShowReportTimeEmp_Click(object sender, EventArgs e)
        {
            //if (CB_Employee.Value != null)
            //{
                string Employee = Session["User"].ToString();
                string Checkin1 = (Convert.ToInt32(CB_YearTimwEmp.Value.ToString()) - 543).ToString() + "/" + CB_MonthTimeEmp.Value + "/01";
                string Checkin2 = (Convert.ToInt32(CB_YearTimwEmp.Value.ToString()) - 543).ToString() + "/" + CB_MonthTimeEmp.Value + "/31";
                DataTable dt = new DataTable();
                dt = GetData_Employee(Employee, Checkin1, Checkin2);
                ClassSalary CsSalary = new ClassSalary();

                DataTable dtYear = CsSalary.CheckDayOfYear(CB_YearTimwEmp.SelectedItem.Text);
                string Year = dtYear.Rows[0]["DayCount"].ToString();
                lb_MonthYear.Text = CB_MonthTimeEmp.SelectedItem.Text + CB_YearTimwEmp.Value;
                lb_Jobpresence.Text = dt.Rows.Count.ToString();

                switch (CB_MonthTimeEmp.Value.ToString())
                {
                    case "01": if (dt.Rows.Count >= 29)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (29 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "02": if (Year == "365")
                        {
                            if (dt.Rows.Count >= 26)
                            {
                                lb_jobAbsence.Text = "0";

                            }
                            else
                            {
                                lb_jobAbsence.Text = (26 - dt.Rows.Count).ToString();

                            }
                        }
                        if (Year == "366")
                        {
                            if (dt.Rows.Count >= 27)
                            {
                                lb_jobAbsence.Text = "0";

                            }
                            else
                            {
                                lb_jobAbsence.Text = (27 - dt.Rows.Count).ToString();

                            }
                        }
                        break;
                    case "03": if (dt.Rows.Count >= 29)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (29 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "04": if (dt.Rows.Count >= 28)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (28 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "05": if (dt.Rows.Count >= 29)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (29 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "06": if (dt.Rows.Count >= 28)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (28 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "07": if (dt.Rows.Count >= 29)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (29 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "08": if (dt.Rows.Count >= 29)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (29 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "09": if (dt.Rows.Count >= 28)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (28 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "10": if (dt.Rows.Count >= 29)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (29 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "11": if (dt.Rows.Count >= 28)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (28 - dt.Rows.Count).ToString();

                        }
                        break;
                    case "12": if (dt.Rows.Count >= 29)
                        {
                            lb_jobAbsence.Text = "0";

                        }
                        else
                        {
                            lb_jobAbsence.Text = (29 - dt.Rows.Count).ToString();

                        }
                        break;

                }

                GV_ShowTimeEmp.DataSource = dt;
                GV_ShowTimeEmp.DataBind();

            }
            //else
            //{
            //    Response.Write(@"<script language='javascript'>alert('กรุณาเลือกพนักงานที่ต้องการ')</script>");
            //}
       // }
        private DataTable GetData_Employee(string Emp,string Ch1,string Ch2)
        {
            string sqltext = @"SELECT * FROM [TB_Time_Cards] WHERE Employee_ID = '"+Emp+"' AND Check_in BETWEEN '"+Ch1+"' AND '"+Ch2+"'";
            SqlConnection sqlconn = new SqlConnection(Connection);
            SqlDataAdapter da = new SqlDataAdapter(sqltext, sqlconn);
            DataSet ds = new DataSet();
            da.Fill(ds);

            return ds.Tables[0];

        }
       
    }
}